package com.saltedfish.tool.dialog;

import android.app.Activity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.saltedfish.tool.R;
import com.saltedfish.tool.dao.SubFlagModelDao;
import com.saltedfish.tool.model.SmallTable;
import com.saltedfish.tool.model.WeekInfo;
import com.saltedfish.tool.ui.flag.adapter.SelectWeekAdapter;
import com.saltedfish.tool.ui.flag.adapter.WeekShowAdapter;
import com.saltedfish.tool.util.DateUtils;
import com.saltedfish.tool.util.UIUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.base.SimpleListener;
import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.popwindow.DialogHelper;
import cn.droidlover.xdroidmvp.util.ToastUtils;
import cn.droidlover.xdroidmvp.view.picker.WheelDateView;

public class UIWeekDialog extends DialogHelper {



    @BindView(R.id.tvBack)
    ImageView tvBack;

    @BindView(R.id.tvDateTitle)
    TextView tvDateTitle;

    @BindView(R.id.tvNoSonFlag)
    TextView tvNoSonFlag;

    @BindView(R.id.whellView1)
    WheelDateView whellView1;

    @BindView(R.id.whellView2)
    WheelDateView whellView2;

    @BindView(R.id.whellView3)
    WheelDateView whellView3;

    @BindView(R.id.whellView4)
    WheelDateView whellView4;

    @BindView(R.id.whellView5)
    WheelDateView whellView5;

    @BindView(R.id.whellView6)
    WheelDateView whellView6;

    @BindView(R.id.whellView7)
    WheelDateView whellView7;

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;

    @BindView(R.id.mTvLeft)
    TextView mTvLeft;
    @BindView(R.id.view_right)
    TextView view_right;

    @BindView(R.id.mFlagRecyclerView)
    RecyclerView mFlagRecyclerView;

    private SmallTable subFlagModel;

    private LinearLayoutManager linearLayoutManager;


    private List<SmallTable> subFlagLists = new ArrayList<>();

    private WeekShowAdapter weekShowAdapter;

    public UIWeekDialog(Activity context) {
        super(context, R.style.AlertDialogStyle);
        this.mContext = context;

        setFullWidthScreen();
        setGravity(Gravity.BOTTOM);
        setAnimSet(R.style.popRightPopAnim);

    }

    public void updateDate(SmallTable subFlagModel, SubFlagModelDao subFlagModelDao) {
        this.subFlagModel = subFlagModel;
//        XLog.e("-重复时间-subFlagModel---->>"+subFlagModel.toString());
        if(!TextUtils.isEmpty(subFlagModel.repeatDay)){
            String[] weeks = subFlagModel.repeatDay.split(" ");
            for (String week : weeks) {
                if(selectList !=null){
                    for (int i = 0; i < selectList.size(); i++) {
                        if(week.equals(selectList.get(i).week)){
                            selectList.get(i).flag = true;
                        }
                    }
                }
            }
//            XLog.e("----selectList---"+selectList.toString());
            if(flagAdapter !=null){
                flagAdapter.setWeekList(selectList);
            }

        }else {
            initData();
        }

        if(!TextUtils.isEmpty(subFlagModel.repeatTime)){
            String[] repeatTimes = subFlagModel.repeatTime.split("-");
            String startTime[] =  repeatTimes[0].split(":");
            String endTime[] =  repeatTimes[1].split(":");
            whellView1.setItems(date1,startTime[0]);
            whellView3.setItems(date3,startTime[1]);
            whellView5.setItems(date5,endTime[0]);
            whellView7.setItems(date7,endTime[1]);
        }else {
            initView();
        }

        if(subFlagModel !=null && subFlagModelDao !=null){
            if(TextUtils.isEmpty(subFlagModel.subId)){
                subFlagLists = subFlagModelDao.getAllByStatusAndProgress(1.0f,"进行中","已延期");
//                List<SmallTable>  list2 = subFlagModelDao.getAllByStatus("delay");
//                subFlagLists.addAll(list);
//                subFlagLists.addAll(list2);

            }else {
                subFlagLists = subFlagModelDao.getAllByStatusAndSb(subFlagModel.subId,1.0f,"进行中","已延期");
//                List<SmallTable>  list2 = subFlagModelDao.getAllByStatus(subFlagModel.subId,"delay");
//                subFlagLists.addAll(list);
//                subFlagLists.addAll(list2);
            }
        //   XLog.e("---进行中的子目标-->>"+subFlagLists.toString());
            if(subFlagLists.size() !=0){
                tvNoSonFlag.setVisibility(View.GONE);
                linearLayoutManager = new LinearLayoutManager(mContext);
                mFlagRecyclerView.setLayoutManager(linearLayoutManager);
                weekShowAdapter = new WeekShowAdapter(subFlagLists,mContext);
                mFlagRecyclerView.setAdapter(weekShowAdapter);

            }else {
                tvNoSonFlag.setVisibility(View.VISIBLE);
            }
        }


    }


    @Override
    protected int getLayoutId() {
        return R.layout.dialog_ui_week;
    }

    private List<String> date1;
    private List<String> date2;
    private List<String> date3;
    private List<String> date4;
    private List<String> date5;
    private List<String> date6;
    private List<String> date7;

    private String hour1;
    private String minute1;
    private String hour2;
    private String minute2;

    @Override
    protected void initView() {
        tvDateTitle.setText("重复&时间");
        date1 = new ArrayList<>();
        date2 = new ArrayList<>();
        date3 = new ArrayList<>();
        date4 = new ArrayList<>();
        date5 = new ArrayList<>();
        date6 = new ArrayList<>();
        date7 = new ArrayList<>();

        date2.add(":");
        date4.add("至");
        date6.add("：");
        for (int i = 0; i <= 23; i++) {
            String index;
            if(i<10){
                index ="0" + i;
            }else{
                index = i +"";
            }
            date1.add(index);
            date5.add(index);
        }

        for (int i = 0; i <= 59; i++) {
            String index;
            if(i<10){
                index ="0" + i;
            }else{
                index = i +"";
            }
            date3.add(index);
            date7.add(index);
        }

        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        whellView4.setTextColor(0XFF333333);

        whellView1.setItemsTwo(date1);
        whellView3.setItemsTwo(date3);
        whellView5.setItemsTwo(date5);
        whellView7.setItemsTwo(date7);

        whellView2.setItems(date2);
        whellView4.setItems(date4);
        whellView6.setItems(date6);

        whellView1.setOnWheelViewListener(new WheelDateView.OnWheelViewListener() {
            @Override
            public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
                hour1 = item;
            }
        });

        whellView3.setOnWheelViewListener(new WheelDateView.OnWheelViewListener() {
            @Override
            public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
                minute1 = item;
            }
        });
        whellView5.setOnWheelViewListener(new WheelDateView.OnWheelViewListener() {
            @Override
            public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
                hour2 = item;
            }
        });

        whellView7.setOnWheelViewListener(new WheelDateView.OnWheelViewListener() {
            @Override
            public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
                minute2 = item;
            }
        });

    }


    List<WeekInfo> selectList;

    private SelectWeekAdapter flagAdapter;

    @Override
    protected void initData() {
        selectList = new ArrayList<>();
        selectList.add(new WeekInfo("周一",false));
        selectList.add(new WeekInfo("周二",false));
        selectList.add(new WeekInfo("周三",false));
        selectList.add(new WeekInfo("周四",false));
        selectList.add(new WeekInfo("周五",false));
        selectList.add(new WeekInfo("周六",false));
        selectList.add(new WeekInfo("周日",false));
//        mTvLeft.measure(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//        int width =  mTvLeft.getMeasuredWidth();
//        view_right.measure(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//        int widthRight =  view_right.getMeasuredWidth();

//        mRecyclerView.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        int widethR =  mRecyclerView.getMeasuredWidth();

        int widths = (UIUtils.getScreenWidth(mContext) - UIUtils.dip2px(mContext,73) - UIUtils.dip2px(mContext,15))/7;
//        XLog.e(widths+"-----width---11--"+UIUtils.dip2px(mContext,28));
        flagAdapter = new SelectWeekAdapter(selectList,mContext,widths);

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,7));
        flagAdapter.setListener(new SimpleListener() {

            @Override
            public void onClick(int position) {
                if(selectList.get(position).flag){
                    selectList.get(position).flag = false;
                }else {
                    selectList.get(position).flag = true;
                }
            }
        });
        mRecyclerView.setAdapter(flagAdapter);


      //  tvNoSonFlag.setVisibility(View.VISIBLE);


    }


    @Override
    protected void dialogDismiss() {
        dismiss();
    }

    @Override
    public void dismiss() {
        mContext.getWindow().setStatusBarColor(mContext.getResources().getColor(R.color.color_white));
        String startTime = hour1 + ":" + minute1;
        String endTime = hour2 + ":" + minute2;
//        int index = DateUtils.compareEqualsHour(startTime, endTime);
//        XLog.e(subFlagModel.toString()+"-index---"+index);

        subFlagModel.repeatTime = startTime +"-" + endTime;


//        if(index == 1){
//            subFlagModel.repeatTime = startTime +"-" + endTime;
//        }else {
//            ToastUtils.showMessage(mContext,"开始时间必须小于结束时间");
//            return;
//        }
        String week = updateWeek();
        subFlagModel.repeatDay = week;
        if(listener !=null){
            listener.onDateClick(subFlagModel);
        }


        super.dismiss();


    }


    private String updateWeek() {
        String weeks = "";
        if(selectList !=null){

            for (WeekInfo weekInfo : selectList) {
                if(weekInfo.flag){
                    if(TextUtils.isEmpty(weeks)){
                        weeks = weekInfo.week;
                    }else {
                        weeks = weeks + " " + weekInfo.week;
                    }
                }
            }

//            for (int i = 0; i < selectList.size(); i++) {
//                WeekInfo weekInfo = selectList.get(i);
//                if(weekInfo.flag){
//                    if(TextUtils.isEmpty(weeks)){
//                        weeks = weekInfo.week;
//                    }else {
//                        weeks = weeks + " " + weekInfo.week;
//                    }
//                }
//            }
        }
//        XLog.e("--weeks--->>"+weeks);
        return weeks;
    }

    private SimpleDateListener listener;

    public static class SimpleDateListener{
        void onDateClick(SmallTable subFlagModel) { }
    }

    public void setListener(SimpleDateListener listener) {
        this.listener = listener;
    }

}
